<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script src="../js/jquery.min.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理
            <small>用户管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="用户名" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findByCondition()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="username" label="用户名" align="center"></el-table-column>
                <el-table-column prop="birthday" label="生日" align="center"></el-table-column>
                <el-table-column prop="gender" label="性别" align="center"></el-table-column>

                <el-table-column prop="telephone" label="联系方式" align="center"></el-table-column>
                <el-table-column prop="remark" label="备注" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="密码">
                                                <el-input v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="联系方式">
                                                <el-input v-model="formData.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <!--生日是单独的一行，测试这种控件方法是否可行-->
                                    <el-row>
                                        <el-col :span="24" >
                                        <el-form-item label="生日">
                                            <el-date-picker
                                                    v-model="formData.birthday"
                                                    align="right"
                                                    type="date"
                                                    placeholder="选择生日"
                                                    :picker-options="pickerOptions">
                                            </el-date-picker>
                                        </el-form-item>
                                    </el-col>
                                    </el-row>
                                    <!--生日是单独的一行，测试这种控件方法是否可行-->
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="说明">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        <el-tab-pane label="授权管理" name="second">
                            <el-transfer
                                v-model="roleIds"
                                :titles="titles"
                                :data="roleList"
                                @change="changeSelectRole"
                            >
                            </el-transfer>
                        </el-tab-pane>

                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="用户信息编辑" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="密码">
                                                <el-input v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="联系方式">
                                                <el-input v-model="formData.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="生日">
                                                <el-date-picker
                                                        v-model="formData.birthday"
                                                        align="right"
                                                        type="date"
                                                        placeholder="选择生日"
                                                        :picker-options="pickerOptions">
                                                </el-date-picker>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="说明">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                </el-form>
                            </el-tab-pane>

                            <el-tab-pane label="授权管理" name="second">
                                <el-transfer
                                        v-model="roleIds"
                                        :titles="titles"
                                        :data="roleList"
                                        @change="changeSelectRole"
                                >
                                </el-transfer>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>


        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            // 左右选择框相关数据
            titles:["未关联角色","已关联角色"],//穿梭框标题
            roleIds:[],//穿梭框右边框中的ids，即已选中的ids
            roleList:[],//左右选择框的数据集合

            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//新增和编辑表单中对应的检查项列表数据
            checkitemIds: [],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏

            pickerOptions: null
        },
        created() {
            this.findPage();
        },
        methods: {
            changeSelectRole(){},
            //编辑
            handleEdit() {
                var _this = this;
                _this.dialogFormVisible4Edit = false;
                //发送异步请求,提交表单数据
                axios.post("/user/edit.do?roleIds=" + _this.roleIds, _this.formData).then((response) => {
                    //关闭弹窗

                    if (response.data.flag) {//提交成功
                        _this.$message({
                            message: response.data.message,
                            type: 'success'
                        });
                    } else {//提交失败
                        _this.$message.error(response.data.message)
                    }
                }).finally(() => {
                    _this.findPage();
                });
            },
            //添加
            handleAdd() {
                var _this = this;
                //发送异步请求,提交表单数据
                axios.post("/user/add.do?roleIds=" + _this.roleIds, _this.formData).then((response) => {
                    //关闭弹窗
                    _this.dialogFormVisible = false;
                    if (response.data.flag) {
                        _this.$message({
                            message: response.data.message,
                            type: 'success'
                        });
                    } else {
                        _this.$message.error(response.data.message)
                    }
                }).finally(() => {
                    _this.findPage();
                })
            },
            //分页查询
            findPage() {
                var _this = this;
                var param = {
                    currentPage: _this.pagination.currentPage,//当前页
                    pageSize: _this.pagination.pageSize,//每页信息数
                    queryString: _this.pagination.queryString,//分页查询条件
                };
                axios.post("/user/findByCondition.do", param).then((response) => {
                    _this.dataList = response.data.rows;//列表数据
                    _this.pagination.total = response.data.total;
                })
            },
            findByCondition() {
                var _this = this;
                _this.pagination.currentPage = 1;
                _this.findPage();
            },
            // 重置表单
            resetForm() {
                var _this = this;
                //重置表单
                _this.formData = {};
                //默认显示第一界面
                _this.activeName = 'first';
                _this.roleIds = [];
                _this.roleList = [];
            },
            // 弹出添加窗口
            handleCreate() {
                var _this = this;
                //重置表单
                _this.resetForm();
                //显示弹窗
                _this.dialogFormVisible = true;
                //发送请求获取所有的角色
                axios.post("/user/findAllRole.do").then((res) => {
                    if (res.data.flag) {
                        // _this.$message.success(res.data.message);
                        //构造左右选择框中的数据
                        res.data.data.forEach((role, index) => {
                            _this.roleList.push({
                                label: role.name,
                                key: role.id
                            });
                        });
                    } else {
                        _this.$message.error(res.data.message);
                    }
                });
                //已经关联的角色
                _this.roleIds=[];
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                var _this = this;
                //发送异步请求,查询当前组信息
                _this.activeName = 'first';
                axios.get("/user/findById.do?id=" + row.id).then((response) => {
                    if (response.data.flag) {
                        //弹出编辑窗口
                        _this.dialogFormVisible4Edit = true;
                        //双向用户数据
                        _this.formData = response.data.data;
                        //发送异步请求,获取所有用户对应的角色信息
                        axios.get("/user/findRoleById.do?id=" + row.id).then((response) => {
                            if (response.data.flag) {
                                //获取所有角色
                                axios.post("/user/findAllRole.do").then((res) => {
                                    if (res.data.flag) {
                                        // _this.$message.success(res.data.message);
                                        //构造左右选择框中的数据
                                        res.data.data.forEach((role, index) => {
                                            _this.roleList.push({
                                                label: role.name,
                                                key: role.id
                                            });
                                        });
                                    } else {
                                        _this.$message.error(res.data.message);
                                    }
                                });
                                //绑定角色信息
                                _this.roleIds = response.data.data;
                            } else {
                                _this.$message.error(response.data.message)
                            }
                        })
                    } else {
                        _this.$message.error(response.data.message)
                    }
                });
                _this.resetForm();
            },
            //切换页码
            handleCurrentChange(currentPage) {
                var _this = this;
                _this.pagination.currentPage = currentPage;
                _this.findPage();
            },
            // 删除
            handleDelete(row) {
                var _this = this;
                //删除前二次确认
                _this.$confirm("你确定要删除当前数据么?", "提示", {type: "warning"}).then(() => {
                    axios.get("/user/deleteById.do?id=" + row.id).then((response) => {
                        if (response.data.flag) {
                            _this.$message({
                                message: response.data.message,
                                type: 'success'
                            })
                        } else {
                            _this.$message.error(response.data.message);
                        }
                    }).finally(() => {
                        _this.findPage();
                    })
                })
            }
        }
    })
</script>


</html>
